package com.ship.contract.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.core.base.BaseController;
import com.ship.common.core.base.Page;
import com.ship.contract.bean.vo.QueryConLawyerVo;
import com.ship.contract.service.ExportService;
import com.ship.common.core.utils.HttpRequestUtil;
import com.ship.common.core.utils.SpringUtil;
import com.ship.common.core.validate.Add;
import com.ship.common.core.validate.Update;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.contract.bean.ConExcellentCases;
import com.ship.contract.bean.ConLawyer;
import com.ship.contract.service.ConLawyerService;
import com.ship.contract.task.ExportContractDataTask;
import com.ship.contract.utils.ModuleConst;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/conLawyer")
public class ConLawyerController extends BaseController {

    @Autowired
    private ConLawyerService conLawyerService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private HttpRequestUtil httpRequestUtil;

    @Autowired
    private SpringUtil springUtil;

    /**
     * 分页
     *
     * @param conLawyer
     * @return
     */
    @PostMapping("/page")
    public JsonResult<Page<ConLawyer>> page(@RequestBody ConLawyer conLawyer) {
        Page<ConLawyer> page = conLawyerService.page(conLawyer,new QueryWrapper<ConLawyer>().ne("del_apply",2));
//        List<ConLawyer> list = page.getList();
//        List<QueryConLawyerVo> conLawyerVoList = new ArrayList<>();
//        if (CollectionUtil.isNotEmpty(list)){
//            list.forEach(item -> {
//                QueryConLawyerVo conLawyerVo = new QueryConLawyerVo();
//                BeanUtils.copyProperties(item,conLawyerVo);
//                if (item.getExpirationDate() != null && item.getPayTime() != null){
//                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//                    LocalDate localDate1 = LocalDate.parse(DateUtil.format(item.getExpirationDate(), DatePattern.NORM_DATE_FORMATTER), formatter);
//                    LocalDate localDate2 = LocalDate.parse(DateUtil.format(item.getPayTime(), DatePattern.NORM_DATE_FORMATTER), formatter);
//                    Period period = Period.between(localDate1, localDate2);
//                    int days = period.getDays();
//                    if (days >= 0){
//                        conLawyerVo.setShowPayTime("合同到期前"+days+"天");
//                    }else {
//                        int abs = Math.abs(days);
//                        conLawyerVo.setShowPayTime("合同签订前"+abs+"天");
//                    }
//
//                }
//                conLawyerVoList.add(conLawyerVo);
//            });
//        }
//        Page<QueryConLawyerVo> page1 = new Page<>();
//        BeanUtils.copyProperties(page,page1);
//        page1.setList(conLawyerVoList);
        return JsonResult.success(page);
    }

    /**
     * 新增
     *
     * @param conLawyer
     * @return
     */
    @PostMapping
    @Log(title = "新增律师合同",businessType = BusinessType.INSERT)
    public JsonResult<Object> add(@RequestBody @Validated(Add.class)ConLawyer conLawyer) {
        conLawyerService.saveCon(conLawyer);
        return JsonResult.success();
    }

    /**
     * 修改
     *
     * @param conLawyer
     * @return
     */
    @PutMapping
    @Log(title = "修改律师合同",businessType = BusinessType.UPDATE)
    public JsonResult<Object> update(@RequestBody @Validated(Update.class)ConLawyer conLawyer) {
        conLawyerService.updateCon(conLawyer);
        return JsonResult.success();
    }

    /**
     * 删除申请
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Log(title = "删除律师合同",businessType = BusinessType.DELETE)
    public JsonResult delete(@PathVariable Long id){
        conLawyerService.delApply(id);
        return JsonResult.success();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping
    public JsonResult<Map<String,Object>> detail(Long id) {
        return JsonResult.success(conLawyerService.detail(id));
    }

    /**
     * 申请优秀案例
     * @param conExcellentCases
     * @return
     */
    @PostMapping("/excellent")
    public JsonResult excellent(@RequestBody ConExcellentCases conExcellentCases){
        conLawyerService.excellent(conExcellentCases, SecurityUtils.getUsername());
        return JsonResult.success();
    }

    /**
     * 导出
     *
     * @param conLawyer
     * @return
     */
    @PostMapping("/export")
    @Log(title = "导出律师合同",businessType = BusinessType.EXPORT)
    public JsonResult<Object> export(@RequestBody ConLawyer conLawyer) {
        ExportContractDataTask task = new ExportContractDataTask();
        task.setBaseBean(conLawyer);
        task.setBaseService(conLawyerService);
        task.setMethodsName("page");
        task.setModule(ModuleConst.CON_LAWYER);
        task.setDownloadFileName("导出律师合同.zip");
        task.setHttpRequestUtil(httpRequestUtil);
        task.setSpringUtil(springUtil);
        task.setUserId(SecurityUtils.getUserId());
        exportService.export(task);
        return JsonResult.success();
    }
}
